package com.fmsz.xmly.asynctasks.findrecom;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.fmsz.xmly.callbacks.InterfaceClass;
import com.fmsz.xmly.entitys.findrecom.FindRecomVP2Entity;
import com.fmsz.xmly.utils.MyHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/5.
 */

public class FindRecomVP2Down extends AsyncTask<String,Void,String> {
    InterfaceClass.FindRecomVP2Callback callback;

    public FindRecomVP2Down(InterfaceClass.FindRecomVP2Callback callback) {
        this.callback = callback;
    }

    @Override
    protected String doInBackground(String... params) {
        String json= MyHttpUtils.getStringFromUrl(params[0]);
        return json;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (!TextUtils.isEmpty(s)) {
            List<FindRecomVP2Entity> list=getList(s);
            callback.findRecomVP2Callback(list);
        }
    }

    private List<FindRecomVP2Entity> getList(String json) {
        List<FindRecomVP2Entity> list=new ArrayList<>();
        try {
            JSONObject obj1=new JSONObject(json);
            JSONObject obj2 = obj1.getJSONObject("discoveryColumns");
            JSONArray list1 = obj2.getJSONArray("list");
            for (int i = 0; i < list1.length(); i++) {
                JSONObject obj3 = list1.getJSONObject(i);
                String title = obj3.getString("title");
                String coverPath = obj3.getString("coverPath");
                String contentType = obj3.getString("contentType");
                if (contentType.equals("album_detail")) {
                    JSONObject properties = obj3.getJSONObject("properties");
                    String albumId = properties.getString("albumId");
                    list.add(new FindRecomVP2Entity(title,coverPath,contentType,albumId));
                }else if (contentType.equals("ranking_list")){
                    JSONObject properties = obj3.getJSONObject("properties");
                    String key = properties.getString("key");
                    list.add(new FindRecomVP2Entity(title,coverPath,contentType,key));
                }else if (contentType.equals("html5.mall")){
                    String url = obj3.getString("url");
                    list.add(new FindRecomVP2Entity(title,coverPath,contentType,url));
                }else if (contentType.equals("html5")){
                    String url = obj3.getString("url");
                    list.add(new FindRecomVP2Entity(title,coverPath,contentType,url));
                } else if (contentType.equals("activity")){
                    list.add(new FindRecomVP2Entity(title,coverPath,contentType,null));
                }else if (contentType.equals("xzone")){
                    list.add(new FindRecomVP2Entity(title,coverPath,contentType,null));
                }
            }
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
